#!/usr/bin/python
import os, sys
import atexit

# test downloadPackageAndOutputFilePath
def shouldfail(cmd):
	if os.system('%s >/dev/null 2>/dev/null'%cmd)==0:
		raise Exception('test failed. command should fail.', cmd)

def shouldsuccess(cmd):
	if os.system('%s >/dev/null 2>/dev/null'%cmd):
		raise Exception('test failed. command should success.', cmd)

def shouldnotexist(filename):
	if os.path.exists(filename):
		raise Exception(filename+' found. I will not overwrite.')

def shouldfailandoutput(cmd, msg):
	if os.system('%s >tmp1 2>&1'%cmd)==0:
		raise Exception('test failed. command should fail.', cmd)
	c=open('tmp1','r').read()
	if not msg in c:
		raise Exception('"%s" not found in output of command.'%msg, cmd, c)

def shouldsuccessandoutputnothing(cmd, outputfile):
	if os.system(cmd): raise Exception('cmd failed.', cmd)
	s=open(outputfile, 'r').read().strip()
	if len(s): raise Exception('output not nothing.', outputfile, s)

testfile='downloadPackageAndOutputFilePath'

if os.path.exists(testfile)==False: raise Exception(testfile+' not found.')
shouldnotexist('urlfile')
shouldnotexist('source_dir')
shouldnotexist('output_filename')
shouldnotexist('notexists')
shouldnotexist('exists')
shouldnotexist('tmp1')
def cleanup():
	if os.system('rm urlfile source_dir output_filename notexists exists tmp1 -rf'): 
		raise Exception
atexit.register(cleanup)
if os.system('touch exists'): raise Exception('cannot touch "exists"')

cmd1='./%s'%testfile

# Accept just three arguments
shouldfail(cmd1)
shouldfail(cmd1+' 1')
shouldfail(cmd1+' 1 2')
shouldfail(cmd1+' 1 2 3 4')

# Check file exists
shouldfailandoutput(cmd1+' notexists exists notexists', 'exists') # arg 1 error
shouldfailandoutput(cmd1+' exists notexists notexists', 'exists') # arg 2 error
shouldfailandoutput(cmd1+' exists exists exists', 'exists') # arg 3 error

if os.system('mkdir source_dir'): raise Exception
# Empty line
open('urlfile','w').write('\n\n\n\n\n\n\n')
shouldsuccessandoutputnothing(cmd1+' urlfile source_dir output_filename', 'output_filename')
if os.system('rm output_filename -f'): raise Exception
# Comment line
open('urlfile','w').write('#\n#\n#\n#\n#\n#\n#\n')
shouldsuccessandoutputnothing(cmd1+' urlfile source_dir output_filename', 'output_filename')
if os.system('rm output_filename -f'): raise Exception
# Bad url, no filename.
open('urlfile','w').write('http://tdt.sjtu.edu.cn/')
shouldfail(cmd1+' urlfile source_dir output_filename')
if os.system('rm output_filename -f'): raise Exception
# Bad url, unknown protocol
open('urlfile','w').write('unknown://tdt.sjtu.edu.cn/')
shouldfail(cmd1+' urlfile source_dir output_filename')
if os.system('rm output_filename -f'): raise Exception
# Notexist file
open('urlfile','w').write('http://tdt.sjtu.edu.cn/notexist/notexist_file')
shouldfail(cmd1+' urlfile source_dir output_filename')
if os.system('rm output_filename -f'): raise Exception
# Should output filepath
shouldnotexist('index.htm')
open('urlfile','w').write('http://tdt.sjtu.edu.cn/index.htm')
shouldsuccess(cmd1+' urlfile source_dir output_filename')
s=open('output_filename').read()
if not 'source_dir/index.htm' in s:
	raise Exception('no source_dir/index.htm in output_filename', s)
if os.system('rm output_filename -f'): raise Exception

print 'downloadPackageAndOutputFilePath passed all tests.'
